/*
Copyright (C) 2007 Kamal Singh.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Leo's podcasts
var podcast_0_url = "http://leoville.tv/podcasts/twit.xml";
var podcast_1_url = "http://leoville.tv/podcasts/kfi.xml";
var podcast_2_url = "http://leoville.tv/podcasts/mbw.xml";
var podcast_3_url = "http://leoville.tv/podcasts/ww.xml";
var podcast_4_url = "http://leoville.tv/podcasts/sn.xml";
var podcast_5_url = "http://leoville.tv/podcasts/itn.xml";
var podcast_6_url = "http://leoville.tv/podcasts/dgw.xml";
var podcast_7_url = "http://leoville.tv/podcasts/floss.xml";
var podcast_8_url = "http://leoville.tv/podcasts/tlr.xml";
var podcast_9_url = "http://leoville.tv/podcasts/fib.xml";
var podcast_10_url = "http://leoville.tv/podcasts/twil.xml";
var podcast_11_url = "http://leoville.tv/podcasts/leo.xml";
var podcast_12_url = "http://leoville.tv/podcasts/jm.xml";
var podcast_13_url = "http://leoville.tv/podcasts/mc.xml";
var podcast_14_url = "http://leoville.tv/podcasts/roz.xml";

var results = new Array();
var curAudioClip_ = WMPEmbed.object;
var blinkIcon = false;

function animateIcon() {
  if (blinkIcon == false) {
    logoButton.image = "logo_down.png";
    blinkIcon = true;
    //podcastList.autoscroll = true;
  }
  else {
    logoButton.image = "logo.png";
    blinkIcon = false;
    //podcastList.autoscroll = false;
  }
}

function View_onOpen() {
  parse(podcast_0_url);
}

// podcast feed parser
function parse(url) {
  var feedRequest = null;
  
  feedRequest = new XMLHttpRequest();
  try {
    feedRequest.open("GET", url, true);
    feedRequest.setRequestHeader("Cache-Control", "no-cache");
    debug.trace("URL : " + url);
  } 
  catch (e) {
    // Catch invalid URLs
    feedRequest = null;
    return;
  }
  
  // Set the callback for when the downloading is completed (or failed)
  feedRequest.onreadystatechange = onFeedLoad;
  
  // Start the download
  try {
    feedRequest.send();
  }
  catch (e) {
    // Catch errors sending the request
    feedRequest = null;
    return;
  }
  
  function onFeedLoad() {
    // Verify that the download completed
    if (feedRequest.readyState != 4)
      return;

    // Verify that the download was successful
    if (feedRequest.status != 200) {
      feedRequest = null;
      return;
    }
    
    var doc = new DOMDocument();
    doc.loadXML(feedRequest.responseText);
    
    var elem = doc.getElementsByTagName("item");
    results = [];
    
    if (elem != null && elem.length > 0) {
      for (var z = 0; z < elem.length; z++) {
        var title, link, mp3Link, duration, summary;
        for (var node = elem[z].firstChild; node != null; node = node.nextSibling) {
          if (node.nodeName == "title") {
            try {
              title = node.firstChild.nodeValue;
            }
            catch (e) {
              title = "";
            }  
          }
          if (node.nodeName == "comments") {
            try {
              link = node.firstChild.nodeValue;
            }
            catch (e) {
              link = "";
            }
          }
          if (node.nodeName == "link") {
            try {
              mp3Link = node.firstChild.nodeValue;
            }            	
            catch (e) {
              mp3Link = "";
            }
          }
          if (node.nodeName == "itunes:duration") {
            try {
              duration = node.firstChild.nodeValue;
            }            	
            catch (e) {
              duration = "";
            }
          }
          if (node.nodeName == "itunes:summary") {
            try {
              summary = node.firstChild.nodeValue;
            }            	
            catch (e) {
              duration = "";
            }
          }
          results[z] = {title:title, link:link, mp3Link:mp3Link, duration:duration, summary:summary};
        }
      }
    }

    displayResults();

    // Destroy the XMLHttpRequest object since it isn't being used anymore
    feedRequest = null;
  }
}

function playClip() {
  if(curAudioClip_ != null) {
    curAudioClip_.controls.play();
  }

  // Update the UI to show this change in play status
  updateInfo();
}

function pauseClip() {
  if(curAudioClip_ != null)
    curAudioClip_.controls.pause();

  // Update the UI to show this change in play status
  updateInfo();
}

var channelNo = 0;

function buttonHandler()  {
  if (channelNo == 14)  // if at last channel, then switch to first channel
    channelNo = 0;
  else
    channelNo = channelNo + 1; // else continue to next channel

  updateImageAndDsec(channelNo); // update the image and desc
  displayResults();
}

function displayResults() {
  podcastList.removeAllElements();  // remove any previous items
  var nItems = results.length;

  for (var i = 0; i < nItems; ++i) {
    var r = results[i];
    var item = podcastList.appendElement('<listitem/>');
    var listlabel = item.appendElement('<label vAlign="top" trimming="word-ellipsis" color="#C0C0C0" underline="false" size="6" font="Bavaria Extended" align="left" height="15" width="100%"></label>');
    listlabel.innerText = r.title;
    listlabel.tooltip = r.title + "\n\n" + r.summary;
  }
}

function timerDisplay() {
  /*try {
    if (curAudioClip_.currentPosition != null) {
      var temp = curAudioClip_.currentPosition;
      minutes = Math.floor(temp/60);  // the minutes
      seconds = temp % 60;            // the seconds
      if (minutes == 0)
        timer.innerText = seconds + " sec";
      else
        timer.innerText = minutes + " min " + seconds + " sec";
    }
  }
  catch(err) {return 0;}*/
  timer.innerText = curAudioClip_.controls.currentPositionString;
  playback_status.innerText = curAudioClip_.status;
}

// React to the user selecting a listitem.
function lb_onchange() {
  // Get the selected listitem
  var selectedItem = podcastList.selectedItem;
  var k = results[podcastList.selectedIndex];
  var audioLink = k.mp3Link;
  if (curAudioClip_ != null && curAudioClip_.playState == 3)
    curAudioClip_.controls.stop();
  onTextClick();
  // Update the UI to show this change in play status
  updateInfo();
  
  function onTextClick() {
    curAudioClip_.URL = audioLink;
    curAudioClip_.controls.play();
    view.setInterval(timerDisplay, 1000);
  }
}

function updateVolume() {
  if( curAudioClip_ == null )
    return;
  curAudioClip_.settings.volume = volumeControl.value;
}

function updateInfo() {
  if (curAudioClip_ != null) {
    playback_status.visible = true;
    playback_status.innerText = curAudioClip_.status;
  }
}

// updating image
function updateImageAndDsec(channelNo) {
  channelImage.src = "images\\podcast_" + channelNo + ".jpg";

  if (channelNo == 0) {
    channelLink.tooltip = "this WEEK in TECH";
    channelDesc.innerText = "Your first podcast of the week is the last word in tech. Join Leo Laporte, Patrick Norton, John C. Dvorak, and other tech luminaries in a roundtable discussion of the latest trends in high tech. Winner of \"People's Choice Podcast\" and \"Best Technology Podcast\" in the 2005 People's Choice Podcast Awards. Released every Sunday at midnight Pacific.";
    channelDesc.tooltip = "Your first podcast of the week is the last word in tech. Join Leo Laporte, Patrick Norton, John C. Dvorak, and other tech luminaries in a roundtable discussion of the latest trends in high tech. Winner of \"People's Choice Podcast\" and \"Best Technology Podcast\" in the 2005 People's Choice Podcast Awards. Released every Sunday at midnight Pacific.";
    channelLink.href = "http://www.twit.tv/twit";
    parse(podcast_0_url);
    channelImage.src = "images/podcast_0.jpg";
  }
  if (channelNo == 1) {
    channelLink.tooltip = "The Tech Guy";
    channelDesc.innerText = "The complete aircheck of Leo Laporte's technology call-in radio show minus commercials, news, and traffic. The show airs Saturdays and Sundays from 2-5p Eastern on the Premiere Radio Networks and XM Channel 152. Check your local listings for a radio station near you.";
    channelDesc.tooltip = "The complete aircheck of Leo Laporte's technology call-in radio show minus commercials, news, and traffic. The show airs Saturdays and Sundays from 2-5p Eastern on the Premiere Radio Networks and XM Channel 152. Check your local listings for a radio station near you.";
    parse(podcast_1_url);
    channelLink.href = "http://www.twit.tv/ttg";
    channelImage.src = "images/podcast_1.jpg";
  }
  if (channelNo == 2) {
    channelLink.tooltip = "MacBreak Weekly";
    channelDesc.innerText = "Get the latest Mac news and views from the top journalists covering Apple today. This roundtable discussion is audio only, and complements the video only MacBreak. Another great show from the Pixel Corps and the TWiT.tv network.";
    channelDesc.tooltip = "Get the latest Mac news and views from the top journalists covering Apple today. This roundtable discussion is audio only, and complements the video only MacBreak. Another great show from the Pixel Corps and the TWiT.tv network.";
    parse(podcast_2_url);
    channelLink.href = "http://www.twit.tv/mbw";
    channelImage.src = "images/podcast_2.jpg";
  }
  if (channelNo == 3) {
    channelLink.tooltip = "Windows Weekly with Paul Thurrott";
    channelDesc.innerText = "A weekly look at all things Microsoft including Windows Vista, Office 2007, and Xbox from the foremost Windows expert in the world, Paul Thurrott of the Super Site for Windows.";
    channelDesc.tooltip = "A weekly look at all things Microsoft including Windows Vista, Office 2007, and Xbox from the foremost Windows expert in the world, Paul Thurrott of the Super Site for Windows.";
    parse(podcast_3_url);
    channelLink.href = "http://www.twit.tv/ww";
    channelImage.src = "images/podcast_3.jpg";
  }
  if (channelNo == 4) {
    channelLink.tooltip = "Security Now";
    channelDesc.innerText = "Steve Gibson, the man who coined the term spyware and created the first anti-spyware program, creator of Spinrite and ShieldsUP, discusses the hot topics in security today with Leo Laporte. Published every Thursday.";
    channelDesc.tooltip = "Steve Gibson, the man who coined the term spyware and created the first anti-spyware program, creator of Spinrite and ShieldsUP, discusses the hot topics in security today with Leo Laporte. Published every Thursday.";
    parse(podcast_4_url);
    channelLink.href = "http://www.twit.tv/sn";
    channelImage.src = "images/podcast_4.jpg";
  }
  if (channelNo == 5) {
    channelLink.tooltip = "net@nite with Amber and Leo";
    channelDesc.innerText = "What's happening on the 'net right now? Amber MacArthur spends every waking moment combing the net for cool sites, viral videos, and funny and moving moments online. Only a fraction of the stuff she finds makes it to her TV shows on Citytv - the rest she shares right here with us.";
    channelDesc.tooltip = "What's happening on the 'net right now? Amber MacArthur spends every waking moment combing the net for cool sites, viral videos, and funny and moving moments online. Only a fraction of the stuff she finds makes it to her TV shows on Citytv - the rest she shares right here with us.";
    parse(podcast_5_url);
    channelLink.href = "http://www.twit.tv/natn";
    channelImage.src = "images/podcast_5.jpg";
  }
  if (channelNo == 6) {
    channelLink.tooltip = "Daily Giz Wiz";
    channelDesc.innerText = "Mad's maddest writer and The Giz Wiz, Dick DeBartolo, digs into his massive gadget collection for the gadget of the day. Released every weekday. This program is co-hosted by Leo Laporte.";
    channelDesc.tooltip = "Mad's maddest writer and The Giz Wiz, Dick DeBartolo, digs into his massive gadget collection for the gadget of the day. Released every weekday. This program is co-hosted by Leo Laporte.";
    parse(podcast_6_url);
    channelLink.href = "http://www.twit.tv/DGW";
    channelImage.src = "images/podcast_6.jpg";
  }
  if (channelNo == 7) {
    channelLink.tooltip = "FLOSS Weekly";
    channelDesc.innerText = "We're not talking dentistry here; FLOSS all about Free Libre Open Source Software. Join hosts Chris DiBona and Leo Laporte every Friday as they talk with the most interesting and important people in the Open Source and Free Software community.";
    channelDesc.tooltip = "We're not talking dentistry here; FLOSS all about Free Libre Open Source Software. Join hosts Chris DiBona and Leo Laporte every Friday as they talk with the most interesting and important people in the Open Source and Free Software community.";
    parse(podcast_7_url);
    channelLink.href = "http://www.twit.tv/FLOSS";
    channelImage.src = "images/podcast_7.jpg";
  }
  if (channelNo == 8) {
    channelLink.tooltip = "The Laporte Report";
    channelDesc.innerText = "The Laporte Report is a compilation of Leo's appearances on other radio shows and podcasts, speeches, lectures, and any other audio that defies categorization.";
    channelDesc.tooltip = "The Laporte Report is a compilation of Leo's appearances on other radio shows and podcasts, speeches, lectures, and any other audio that defies categorization.";
    parse(podcast_8_url);
    channelLink.href = "http://www.twit.tv/TLR";
    channelImage.src = "images/podcast_8.jpg";
  }
  if (channelNo == 9) {
    channelLink.tooltip = "Futures in Biotech";
    channelDesc.innerText = "Explore the world of genetics, cloning, protein folding, genome mapping, and more with the most important researchers in biotech. Hosted by Marc Pelletier and Leo Laporte. Released most Wednesdays.";
    channelDesc.tooltip = "Explore the world of genetics, cloning, protein folding, genome mapping, and more with the most important researchers in biotech. Hosted by Marc Pelletier and Leo Laporte. Released most Wednesdays.";
    parse(podcast_9_url);
    channelLink.href = "http://www.twit.tv/FIB";
    channelImage.src = "images/podcast_9.jpg";
  }
  if (channelNo == 10) {
    channelLink.tooltip = "this WEEK in LAW";
    channelDesc.innerText = "Join Denise Howell and the TWiL panel, including Cathy Kirkman, Ernie Svenson, and John Palfrey as they discuss breaking issues in technology law including patents, copyrights, and more.";
    channelDesc.tooltip = "Join Denise Howell and the TWiL panel, including Cathy Kirkman, Ernie Svenson, and John Palfrey as they discuss breaking issues in technology law including patents, copyrights, and more.";
    parse(podcast_10_url);
    channelLink.href = "http://www.twit.tv/twil";
    channelImage.src = "images/podcast_10.jpg";
  }
  if (channelNo == 11) {
    channelLink.tooltip = "Radio Leo";
    channelDesc.innerText = "A compendium of netcasts from the Chief TWiT, Leo Laporte. This feed includes all his audio releases and often includes his appearances on other netcasts. One feed to serve them all.";
    channelDesc.tooltip = "A compendium of netcasts from the Chief TWiT, Leo Laporte. This feed includes all his audio releases and often includes his appearances on other netcasts. One feed to serve them all.";
    parse(podcast_11_url);
    channelLink.href = "http://www.twit.tv/radio_leo";
    channelImage.src = "images/podcast_11.jpg";
  }
  if (channelNo == 12) {
    channelLink.tooltip = "Jumping Monkeys";
    channelDesc.innerText = "Join Megan Morrone, mother of three toddlers, and Leo Laporte, father of two teens, as they talk about parenting in the digital age, featuring lots of helpful links and advice, plus interviews with other geek parents, web site designers, and bloggers. Another TWiT.tv podcast.";
    channelDesc.tooltip = "Join Megan Morrone, mother of three toddlers, and Leo Laporte, father of two teens, as they talk about parenting in the digital age, featuring lots of helpful links and advice, plus interviews with other geek parents, web site designers, and bloggers. Another TWiT.tv podcast.";
    parse(podcast_12_url);
    channelLink.href = "http://www.twit.tv/jm";
    channelImage.src = "images/podcast_12.jpg";
  }
  if (channelNo == 13) {
    channelLink.tooltip = "Munchcast";
    channelDesc.innerText = "Your weekly trip down junk food lane with Cammy Blackstone and Leo Laporte. Another tasty netcast from TWiT.tv.";
    channelDesc.tooltip = "Your weekly trip down junk food lane with Cammy Blackstone and Leo Laporte. Another tasty netcast from TWiT.tv.";
    parse(podcast_13_url);
    channelLink.href = "http://www.twit.tv/mc";
    channelImage.src = "images/podcast_13.jpg";
  }
  if (channelNo == 14) {
    channelLink.tooltip = "Roz Rows The Pacific";
    channelDesc.innerText = "Join adventurer Roz Savage as she attempts to be the first woman to row solo across the Pacific ocean..";
    channelDesc.tooltip = "Join adventurer Roz Savage as she attempts to be the first woman to row solo across the Pacific ocean..";
    parse(podcast_13_url);
    channelLink.href = "http://twit.tv/roz";
    channelImage.src = "images/podcast_14.jpg";
  }
}